/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Article接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： ArticleRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetCoreCMS.Common;
using System.Data;
using System.Transactions;

namespace NetCoreCMS.Repository.SqlServer
{
    public class ArticleRepository:BaseRepository<Article,Int32>, IArticleRepository
    {
        public ArticleRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public bool DeleteAll(int id, string env)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
                {
                    //取得相册MODEL
                    List<Models.Article_albums> albumsList = _dbConnection.GetList<Models.Article_albums>("where article_id=@id", new { id = id }).ToList();

                    //删除图片相册
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("delete from [" + nameof(Article_albums) + "] where article_id=@article_id;");
                    //删除用户组价格           
                    strSql.Append("delete from [" + nameof(User_group_price) + "] where article_id=@article_id;");
                    //删除商品价格
                    strSql.Append("delete from [" + nameof(Article_goods) + "] where article_id=@article_id;");
                    //删除商品规格
                    strSql.Append("delete from [" + nameof(Article_goods_spec) + "] where article_id=@article_id;");
                    //删除评论
                    strSql.Append("delete from [" + nameof(Article_comment) + "] where article_id=@article_id;");
                    //删除主表
                    strSql.Append("delete from [" + nameof(Article) + "] where id=@article_id");
                    int rowsAffected = _dbConnection.Execute(strSql.ToString(), new { article_id = id });
                    scope.Complete();
                    if (rowsAffected > 0)
                    {
                        //删除图片
                        foreach (Models.Article_albums modelt in albumsList)
                        {
                            FileHelper.DeleteFile(modelt.thumb_path, env);
                            FileHelper.DeleteFile(modelt.original_path, env);
                        }
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        public bool UpdateField(int id, string strValue)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Article) + "] set " + strValue + " where id=@id", new { id = id }) > 0;
            }
        }
    }
}